self.addEventListener(
  "message",
  function (e) {
    self.matrixData = e.data.matrixData;
    // let basicCanvasPosition = GetOkPosition(e.data.position);
    let basicCanvasPosition = newGetOkPosition(e.data.position);
    self.postMessage({
      basicCanvasPosition: basicCanvasPosition,
    });
  },
  false
);
function newGetOkPosition({
  basicCanvasPosition,
  parCanvasPosition,
  direction,
}) {
  let bool = true;
  //初始位置
  let firstBasic = JSON.parse(JSON.stringify(basicCanvasPosition));
  //先计算上下左右的点位合适不合适 在处理循环过程中不在上下左右中的点位
  let secondForPoint = []; //第二轮要循环的点位
  let currentPosition = null;
  if (direction.split("-")[1] == "top") {
    currentPosition = "up";
    basicCanvasPosition.sColumn = basicCanvasPosition.sColumn + 5;
    basicCanvasPosition.eColumn = basicCanvasPosition.eColumn + 5;
    // basicCanvasPosition.sRow = basicCanvasPosition.sRow - 5;
    // basicCanvasPosition.eRow = basicCanvasPosition.eRow - 5;
  } else if (direction.split("-")[1] == "bottom") {
    currentPosition = "right";
    basicCanvasPosition.sRow = basicCanvasPosition.sRow + 5;
    basicCanvasPosition.eRow = basicCanvasPosition.eRow + 5;
  }
  bool = isUpperBlack(basicCanvasPosition);
  while (bool) {
    // 到达初始位置的时候在重置序号基准位置,然后吧basicCanvasPosition整体左移一个位置或者右移自己的位置
    if (isSameObj(firstBasic, basicCanvasPosition)) {
      let end = false;
      for (let i = 0; i < secondForPoint.length; i++) {
        bool = isUpperBlack(secondForPoint[i]);
        if (!bool) {
          end = true;
          basicCanvasPosition = secondForPoint[i];
          break;
        }
      }
      if (end) continue;
      //重置基准
      secondForPoint = [];
      parCanvasPosition = whileInResetParCanvasPosition(
        basicCanvasPosition,
        parCanvasPosition
      );
      basicCanvasPosition = whileInResetBasicCanvasPosition(
        basicCanvasPosition,
        direction
      );
      firstBasic = JSON.parse(JSON.stringify(basicCanvasPosition));
      if (direction.split("-")[1] == "top") {
        currentPosition = "left";
        basicCanvasPosition.sRow = basicCanvasPosition.sRow - 5;
        basicCanvasPosition.eRow = basicCanvasPosition.eRow - 5;
      } else if (direction.split("-")[1] == "bottom") {
        currentPosition = "right";
        basicCanvasPosition.sRow = basicCanvasPosition.sRow + 5;
        basicCanvasPosition.eRow = basicCanvasPosition.eRow + 5;
      }
      continue;
    }
    let move = getWH(basicCanvasPosition); //要移动的宽高
    let basic = getWH(parCanvasPosition); //循环中心的宽高
    //在左边从下往上找
    if (currentPosition == "left") {
      if (basicCanvasPosition.eRow <= parCanvasPosition.sRow - 1) {
        currentPosition = "up";
        continue;
      }
      basicCanvasPosition.sRow = basicCanvasPosition.sRow - 5;
      basicCanvasPosition.eRow = basicCanvasPosition.eRow - 5;
      let value = leftOrRight(
        move,
        basic,
        basicCanvasPosition,
        parCanvasPosition,
        secondForPoint
      );
      if (!value) {
        continue;
      }
      bool = isUpperBlack(basicCanvasPosition);
      //说明从在基准左边从下往上移动已经和基准的开始位置重合了 , 该往右走了
    }
    //在右边从上往下找
    else if (currentPosition == "right") {
      if (basicCanvasPosition.sRow >= parCanvasPosition.eRow + 1) {
        currentPosition = "down";
        continue;
      }
      basicCanvasPosition.sRow = basicCanvasPosition.sRow + 5;
      basicCanvasPosition.eRow = basicCanvasPosition.eRow + 5;
      let value = leftOrRight(
        move,
        basic,
        basicCanvasPosition,
        parCanvasPosition,
        secondForPoint
      );
      if (!value) {
        continue;
      }
      bool = isUpperBlack(basicCanvasPosition);
    }
    // 在上边从左往右找
    else if (currentPosition == "up") {
      if (basicCanvasPosition.sColumn >= parCanvasPosition.eColumn + 1) {
        currentPosition = "right";
        continue;
      }
      basicCanvasPosition.sColumn = basicCanvasPosition.sColumn + 5;
      basicCanvasPosition.eColumn = basicCanvasPosition.eColumn + 5;
      let value = topOrBottom(
        move,
        basic,
        basicCanvasPosition,
        parCanvasPosition,
        secondForPoint
      );
      if (!value) {
        continue;
      }
      bool = isUpperBlack(basicCanvasPosition);
    }
    //在下边从右往左找
    else if (currentPosition == "down") {
      if (basicCanvasPosition.eColumn <= parCanvasPosition.sColumn - 1) {
        currentPosition = "left";
        continue;
      }
      basicCanvasPosition.sColumn = basicCanvasPosition.sColumn - 5;
      basicCanvasPosition.eColumn = basicCanvasPosition.eColumn - 5;
      let value = topOrBottom(
        move,
        basic,
        basicCanvasPosition,
        parCanvasPosition,
        secondForPoint
      );
      if (!value) {
        continue;
      }
      bool = isUpperBlack(basicCanvasPosition);
    }
  }
  return basicCanvasPosition;
}

function isUpperBlack({ sRow, eRow, sColumn, eColumn }) {
  let maxNum = Math.round((eRow - sRow + 1) * (eColumn - sColumn + 1) * 0.1);
  let OneNum = 0;
  let bool = false;
  for (let i = sRow; i <= eRow; i++) {
    if (bool) {
      break;
    }
    for (let j = sColumn; j <= eColumn; j++) {
      if (
        self.matrixData[i] &&
        typeof self.matrixData[i][j] !== "undefined" &&
        (self.matrixData[i][j] == 2 || self.matrixData[i][j] == 1)
      ) {
        bool = true;
        break;
      }
      if (
        self.matrixData[i] &&
        typeof self.matrixData[i][j] !== "undefined" &&
        self.matrixData[i][j] == 1
      ) {
        OneNum++;
        if (maxNum < OneNum) {
          bool = true;
          break;
        }
      }
    }
  }
  return bool;
}

function isSameObj(obj1, obj2) {
  return (
    obj1.sRow == obj2.sRow &&
    obj1.eRow == obj2.eRow &&
    obj1.sColumn == obj2.sColumn &&
    obj1.eColumn == obj2.eColumn
  );
}

//辅助方法--重新设置基准序号在canvas中的位置
function whileInResetParCanvasPosition(basicCanvasPosition, parCanvasPosition) {
  return {
    sRow: parCanvasPosition.sRow - 5,
    eRow: parCanvasPosition.eRow + 5,
    sColumn: parCanvasPosition.sColumn - 5,
    eColumn: parCanvasPosition.eColumn + 5,
  };
}
//辅助方法--重新设置文本标签在canvas中的位置
// function whileInResetBasicCanvasPosition(basicCanvasPosition, direction) {
//   if (direction.split("-")[1] == "top") {
//     return {
//       sRow: basicCanvasPosition.sRow,
//       eRow: basicCanvasPosition.eRow,
//       sColumn: basicCanvasPosition.sColumn - 5,
//       eColumn: basicCanvasPosition.eColumn - 5,
//     };
//   } else if (direction.split("-")[1] == "bottom") {
//     return {
//       sRow: basicCanvasPosition.sRow,
//       eRow: basicCanvasPosition.eRow,
//       sColumn: basicCanvasPosition.sColumn + 5,
//       eColumn: basicCanvasPosition.eColumn + 5,
//     };
//   }
// }
//辅助方法--重新设置文本标签在canvas中的位置
function whileInResetBasicCanvasPosition(basicCanvasPosition, direction) {
  if (direction.split("-")[1] == "top") {
    return {
      sRow: basicCanvasPosition.sRow - 5,
      eRow: basicCanvasPosition.eRow - 5,
      sColumn: basicCanvasPosition.sColumn,
      eColumn: basicCanvasPosition.eColumn,
    };
  } else if (direction.split("-")[1] == "bottom") {
    return {
      sRow: basicCanvasPosition.sRow,
      eRow: basicCanvasPosition.eRow,
      sColumn: basicCanvasPosition.sColumn + 5,
      eColumn: basicCanvasPosition.eColumn + 5,
    };
  }
}
//辅助方法--得到宽高
function getWH(position) {
  let w = position.eColumn - position.sColumn + 1;
  let h = position.eRow - position.sRow + 1;
  return {
    w,
    h,
  };
}

function leftOrRight(
  move,
  basic,
  basicCanvasPosition,
  parCanvasPosition,
  secondForPoint
) {
  if (move.h <= basic.h) {
    if (
      basicCanvasPosition.sRow < parCanvasPosition.sRow ||
      basicCanvasPosition.eRow > parCanvasPosition.eRow
    ) {
      secondForPoint.push(JSON.parse(JSON.stringify(basicCanvasPosition)));
      return false;
    }
  } else {
    if (
      basicCanvasPosition.eRow < parCanvasPosition.eRow ||
      basicCanvasPosition.sRow > parCanvasPosition.sRow
    ) {
      secondForPoint.push(JSON.parse(JSON.stringify(basicCanvasPosition)));
      return false;
    }
  }
  return true;
}

function topOrBottom(
  move,
  basic,
  basicCanvasPosition,
  parCanvasPosition,
  secondForPoint
) {
  if (move.w <= basic.w) {
    if (
      basicCanvasPosition.sColumn < parCanvasPosition.sColumn ||
      basicCanvasPosition.eColumn > parCanvasPosition.eColumn
    ) {
      secondForPoint.push(JSON.parse(JSON.stringify(basicCanvasPosition)));
      return false;
    }
  } else {
    if (
      basicCanvasPosition.eColumn < parCanvasPosition.eColumn ||
      basicCanvasPosition.sColumn > parCanvasPosition.sColumn
    ) {
      secondForPoint.push(JSON.parse(JSON.stringify(basicCanvasPosition)));
      return false;
    }
  }

  return true;
}
